<?php
class shop extends base{

	public function init(){
		parent::init();

		$this->current = 'my';
		$this->tit = '门店';

		$this->shopObj = D('company_shop');
		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');
		$this->deviceObj = D('company_device');
		$this->adminObj = D('company_admin');
		
		// $this->smsObj = D('sms');

		$this->auth_list = $this->adminObj->get_auth_list($this->admin);
		$this->areas = $this->auth_list['areas'];
		$this->cates = $this->auth_list['cates'];
		$this->zones = $this->auth_list['zones'];
		
	}


	public function index(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,'loaddata', $arrBase);
		
	}

	//loaddata
	public function loaddata(){
		
		$cond = "1=1 and admin_id={$this->admin_id}";

		switch($this->tab){
			case '1'://管理员
				$cond .= ' and status=1';
				break;
			case '2'://
				$cond .= ' and status=0';
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (name like '%".$this->keyword."%' ) ";
		}
		
		$result = $this->shopObj->get_loaddata($cond);
		if($result){
			$this->list = $result['list'];
		}else{
			die('0');
		}
	}

	public function add(){

		if($this->fmdo == 'add'){

			$data['area_id'] = arg('area_id');
        	$data['cate_id'] = arg('cate_id');
        	$data['zone_id'] = arg('zone_id');

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'name' => arg('name'),
				'area_id' => arg('area_id'),
				'cate_id' => arg('cate_id'),
				'zone_id' => arg('zone_id'),
				'tel' => arg('tel'),
				'address' => arg('address'),
				'lng' => arg('lng'),
				'lat' => arg('lat'),
				'intro' => arg('intro'),
				'photo' => joinStr(arg('photo')),
				'status' => 1,
				'add_time' => date('Y-m-d H:i:s'),
			);
			
			$shop_id = $this->shopObj->add($arrBase);
			$this->renderSuccess('添加成功',"/{$this->routeUrl}/index?tab=1");
		}


		$this->info = array('status'=>1);
		$this->display($this->routeUrl."/set.php");
		
	}

	public function edit(){

		$id = arg('id');
		if($this->fmdo == 'edit'){

			$data['area_id'] = arg('area_id');
        	$data['cate_id'] = arg('cate_id');
        	$data['zone_id'] = arg('zone_id');

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'name' => arg('name'),
				'area_id' => arg('area_id'),
				'cate_id' => arg('cate_id'),
				'zone_id' => arg('zone_id'),
				'tel' => arg('tel'),
				'address' => arg('address'),
				'lng' => arg('lng'),
				'lat' => arg('lat'),
				'intro' => arg('intro'),
				'photo' => joinStr(arg('photo')),
				'status' => 1,
				'add_time' => date('Y-m-d H:i:s'),
			);
			
			$shop_id = $this->shopObj->edit($id,$arrBase);
			$this->renderSuccess('编辑成功',"/{$this->routeUrl}/index?tab=1");
		}

		$this->info = $this->shopObj->get_detail($id);
		$this->photos = $this->info['photos'];
		// dump($this->photos);exit;
		$this->display($this->routeUrl."/set.php");
		
	}

}